<?php 
/*
*  A Registry Example for Automatically
*  getInstance() operations.
*  Ersin Güvenç.
*/

/* Our interface */
abstract class Registry
{
    abstract protected function get($key);
    //get stored object.
    abstract protected function set($key,$val);
    //set object.(store object)
} 

Class Registry_Registry extends Registry {

    /**
    * Registry array of objects
    * @access private
    */
    private static $objects = array();

    /**
    * The instance of the registry
    * @access private
    */
    private static $instance;

    //prevent directly access.
    private function __construct(){}

    //prevent clone.
    public function __clone(){}

    /**
    * singleton method used to access the object
    * @access public
    */
    public static function singleton()
    {
        if(!isset(self::$instance))
        {
        self::$instance = new self();
        //echo "new instance\n";
        } else {
        //echo "old instance\n";
        }
        return self::$instance;
    }

    /*
    *  get object protected function you can use only
    *  inside from this class.
    */
    protected function get($key)
    {
        if(isset($this->objects[$key]))
        {
            return $this->objects[$key];
        }
        return NULL;
    }

    /*
    *  set object protected function you can use only
    *  inside from this class.
    */
    protected function set($key,$val)
    {
        $this->objects[$key] = $val;
    }

    /*
    *  get stored object
    */
    static function getObject($key)
    {

        return self::singleton()->get($key);
    }

    /*
    *  store object
    */
    static function storeObject($key, $instance)
    {

        return self::singleton()->set($key,$instance);
    }

} //end of the class.

